package cn.fscode.demo.controller;

import cn.fscode.commons.core.response.Resp;
import cn.fscode.commons.tool.core.exception.Assert;
import com.alibaba.fastjson2.JSONObject;
import org.apache.commons.io.FileUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Map;

/**
 * 主要适配前端, 因为前端是基于jeecgboot开发的, 需要登录以及动态获取菜单和用户信息
 * , 为了前端改动最小, 这里需要实现对应的接口, 可以读取本地json数据即可
 * @author shenguangyang
 */
@RestController
@RequestMapping("/api")
public class SystemController {

    public JSONObject readJson(String fileName) {
        //获取文件的URL
        URL url = this.getClass().getClassLoader().getResource("system/" + fileName + ".json");
        try {
            Assert.notNull(url, "url is null");
            String content = FileUtils.readFileToString(new File(url.getPath()), StandardCharsets.UTF_8);
            return JSONObject.parseObject(content);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }


    @GetMapping("/sys/menu/getUserPermission")
    public Resp<JSONObject> getUserPermission(Map<Object, Object> data) {
        return Resp.ok(readJson("menu-getUserPermission"));
    }

    @GetMapping("/captcha/image")
    public Resp<JSONObject> captchaImage(JSONObject data) {
        return Resp.ok(readJson("captcha-image"));
    }

    @GetMapping("/sys/user/profile/getUserInfo")
    public Resp<JSONObject> getUserInfo(Map<Object, Object> data) {
        return Resp.ok(readJson("user-getUserInfo"));
    }

    @GetMapping("/sys/dict/listAllDictItem")
    public Resp<JSONObject> listAllDictItem(Map<Object, Object> data) {
        return Resp.ok(readJson("listAllDictItem"));
    }

    @PostMapping("/auth/oauth2/token")
    public Resp<JSONObject> login(Map<Object, Object> data) {
        return Resp.ok(readJson("auth-oauth-token"));
    }

    @DeleteMapping("/auth/token/logout")
    public Resp<?> logout(Map<Object, Object> data) {
        return Resp.ok();
    }
}
